import os
from PIL import Image

def is_image_file(file_path):
    try:
        # 使用Pillow库尝试打开文件，如果成功则为图片文件
        with Image.open(file_path):
            return True
    except Exception as e:
        return False

def rename_images_in_folder(folder_path, prefix=""):
    # 获取文件夹中的所有文件
    image_files = [f for f in os.listdir(folder_path) if is_image_file(os.path.join(folder_path, f))]

    # 遍历图片文件并重命名
    for i, image_file in enumerate(image_files):
        # 获取文件的扩展名
        file_extension = os.path.splitext(image_file)[-1].lower()

        # 新的文件名，添加了前缀
        new_file_name = f"{prefix}{i + 1}{file_extension}"

        # 构建新的文件路径
        new_file_path = os.path.join(folder_path, new_file_name)

        # 重命名文件
        os.rename(os.path.join(folder_path, image_file), new_file_path)

    print(f"文件夹 {folder_path} 中的图片文件已重命名完成")

# 调用函数并传入文件夹路径和前缀
image_folder = '/path/to/your/image/folder'
rename_images_in_folder(image_folder, prefix="")
